home *** CD-ROM | disk | FTP | other *** search
- Path: news2.near.net!ceylon!news
- From: Tony Confrey <ac11@gte.com>
- Newsgroups: comp.lang.c++
- Subject: Re: sprintf() in C++
- Date: Thu, 11 Apr 1996 10:03:41 -0400
- Organization: GTE Labs
- Message-ID: <316D113D.167E@gte.com>
- References: <4kene4$nkb@post.gsfc.nasa.gov>
- NNTP-Posting-Host: sambuca.gte.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; AIX 2)
- CC: vonrosen@nssdca.gsfc.nasa.gov
-
- tycho wrote:
- >
- > I could use some help with regard to sprintf() in C++. It seems that
- > cout and cin were invented to avoid printf() and scanf() because the
- > latter have variable argument lists and hence can't be checked against
- > function prototypes. But is there any corresponding C++ approach to
- > sprintf() and sscanf()? When I look at various string class functions
- > I don't see what to use instead of sprintf() and sscanf(). But when I
- > look at my C code, I'm using sprintf() and sscanf() all the time.
- ...
- >
- > Tycho von Rosenvinge
-
- Check out "C++ IOStreams Handbook" by Steve Teale, Addison Wesley.
- It goes into more detail than you'd ever want to know wrt iostreams.
-
- The short answer to your question is that cin and cout are instances
- of the istream and ostream classes which deal with standard input
- and output. There are also ifstream and ofstream classes that deal with
- input and output on files, eg:
-
- ofstream fout ("myfile.txt");
- fout << "Output to a file." << endl;
-
- But what you're looking for is istrstream and ostrstream classes -
- input and output to memory buffers, eg:
-
- char myarray[128];
- ostrstream os(myarray, 128);
- os << "Output to an in memory array!" << ends; // Note ends not endl
-
- If you just want to do the standard stuff (not write your own
- stream class) then the first 3 chapters of Teale's book give you all
- the information you need.
-
-
- Tony
-